package com.punai.datapush.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.domain.CommonConstants;
import com.punai.common.utils.SecurityUtils;
import com.punai.datapush.domain.vo.ReleaseCaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.datapush.mapper.DataReleaseCaseMapper;
import com.punai.datapush.domain.DataReleaseCase;
import com.punai.datapush.service.IDataReleaseCaseService;

import java.util.Date;
import java.util.List;

/**
 * 案例发布Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-27
 */
@Service
public class DataReleaseCaseServiceImpl extends ServiceImpl<DataReleaseCaseMapper,DataReleaseCase> implements IDataReleaseCaseService
{
    @Autowired
    private DataReleaseCaseMapper dataReleaseCaseMapper;

    @Override
    public List<?> selectList(ReleaseCaseVo vo) {
        List<DataReleaseCase> dataReleaseCases = dataReleaseCaseMapper.selectList(
                Wrappers.<DataReleaseCase>lambdaQuery()
                        .like(StrUtil.isNotBlank(vo.getReleaseName()),DataReleaseCase::getReleaseName, "%" + vo.getReleaseName() + "%")
                        .ge(!ObjectUtil.isNull( vo.getStart()),DataReleaseCase::getReleaseDate, vo.getStart())
                        .le(!ObjectUtil.isNull( vo.getEnd()),DataReleaseCase::getReleaseDate, vo.getEnd())
                        .eq(StrUtil.isNotBlank(vo.getReleaseState()),DataReleaseCase::getReleaseState, vo.getReleaseState())
        );
        return dataReleaseCases;
    }

    @Override
    public boolean release(String id) {
        DataReleaseCase dataReleaseCase = dataReleaseCaseMapper.selectById(id);
        dataReleaseCase.setReleaseState(CommonConstants.YES);
        dataReleaseCase.setReleaseDate(new Date());
        dataReleaseCase.setReleaseUser(SecurityUtils.getLoginUser().getUser().getNickName());
        int i = dataReleaseCaseMapper.updateById(dataReleaseCase);
        return i > 0;
    }

    @Override
    public boolean checkDuplicationName(ReleaseCaseVo vo) {
        Long aLong = dataReleaseCaseMapper.selectCount(
                Wrappers.<DataReleaseCase>lambdaQuery()
                        .eq(DataReleaseCase::getReleaseName, vo.getReleaseName())
                        .ne(StrUtil.isNotEmpty(vo.getId()), DataReleaseCase::getId, vo.getId())
        );
        return aLong > 0;
    }

    @Override
    public boolean add(ReleaseCaseVo vo) {
        DataReleaseCase dataReleaseCase = new DataReleaseCase();
        dataReleaseCase.setReleaseName(vo.getReleaseName());
        dataReleaseCase.setReleaseState(CommonConstants.NO);
        int insert = dataReleaseCaseMapper.insert(dataReleaseCase);
        return insert > 0;
    }

    @Override
    public boolean edit(ReleaseCaseVo vo) {
        DataReleaseCase dataReleaseCase = dataReleaseCaseMapper.selectById(vo.getId());
        dataReleaseCase.setReleaseName(vo.getReleaseName());
        int i = dataReleaseCaseMapper.updateById(dataReleaseCase);
        return i > 0;
    }

    @Override
    public boolean repealRelease(String id) {
        DataReleaseCase dataReleaseCase = dataReleaseCaseMapper.selectById(id);
        int i = dataReleaseCaseMapper.update(dataReleaseCase,
                Wrappers.<DataReleaseCase>lambdaUpdate()
                        .set(DataReleaseCase::getReleaseState, CommonConstants.YES_GO_NO)
                        .set(DataReleaseCase::getReleaseDate, null)
                        .set(DataReleaseCase::getReleaseUser, null)
                        .eq(DataReleaseCase::getId, id)
        );
        return i > 0;
    }

    @Override
    public boolean saveCaseSelect(String id, String caseIds) {
        DataReleaseCase dataReleaseCase = dataReleaseCaseMapper.selectById(id);
        dataReleaseCase.setLinkIds(caseIds);
        int i = dataReleaseCaseMapper.updateById(dataReleaseCase);
        return i > 0;
    }

    @Override
    public boolean saveCaseSelectRelease(String id, String caseIds) {
        DataReleaseCase dataReleaseCase = dataReleaseCaseMapper.selectById(id);
        dataReleaseCase.setReleaseState(CommonConstants.YES);
        dataReleaseCase.setReleaseDate(new Date());
        dataReleaseCase.setLinkIds(caseIds);
        dataReleaseCase.setReleaseUser(SecurityUtils.getLoginUser().getUser().getNickName());
        int i = dataReleaseCaseMapper.updateById(dataReleaseCase);
        return i > 0;
    }

    @Override
    public boolean checkShow(String id) {
        DataReleaseCase dataReleaseCase = dataReleaseCaseMapper.selectById(id);
        return StrUtil.isNotBlank(dataReleaseCase.getLinkIds());
    }
}
